package com.ruoyi.web.controller.payment;


import com.ruoyi.common.utils.weiqing.shareUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.weiqing.HttpReqUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/fenxiang")
@CrossOrigin
public class WeixinShareController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    //appid wx775ca6325a3255ef

    public static final String APP_ID = "wx7655a94ba378caae";
    //appsecret   秘钥2675d7e74ae6813a5fdded38c3ae9120
    public static final String SECRET = "26074916cd6eedadbc41e22b0ddf093c";


    /**
     * 获取微信签名
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getSignature", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> WeixinController(HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");

        Map<String, String> ret = new HashMap<>();

        //获取前台传来的三个参数
        String timestamp = "ymwmyhumeqb";
        String nonce_str = "1612195044";
        String url = "http://localhost:8083/";


//        String timestamp = request.getParameter("timestamp");
//        String nonce_str = request.getParameter("nonce_str");
//        String url = request.getParameter("url");
        logger.info("url" + url + "===============" + nonce_str + "============" + timestamp);

        //从缓存中读取token信息，如果没有则获取一个新的token，通过token获取ticket信息
        String access_token = (String) request.getSession().getAttribute("access_token");
        System.err.println("*****************access_token***************"+access_token);
        if (access_token == null) {

            /** 获取AccessToKen*/
            String getAccessToKen = "https://api.weixin.qq.com/cgi-bin/token?"
                    + "grant_type=client_credential&appid=" + APP_ID + "&secret=" + SECRET;
            JSONObject jsonObject = HttpReqUtil.HttpsDefaultExecute("GET", getAccessToKen, null, null, null, JSONObject.class);
            System.err.println("*****************jsonObject***************"+jsonObject);
            /**获取jsapi_ticket*/
            String getTicket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + jsonObject.get("access_token").toString() + "&type=jsapi";
            System.err.println("*****************getTicket***************"+getTicket);
            JSONObject rest = HttpReqUtil.HttpsDefaultExecute("GET", getTicket, null, null, null, JSONObject.class);

            /**将信息保存入缓存中*/
            request.getSession().setAttribute("token", jsonObject.get("access_token").toString());
            request.getSession().setAttribute("ticket", rest.get("ticket").toString());
        }
        /**生成签名*/
        String ticket = (String) request.getSession().getAttribute("ticket"); //微信返回的ticket
        String signature = shareUtil.getSignature(ticket, url, nonce_str, timestamp); //获取签名

        ret.put("nonceStr", nonce_str);
        ret.put("timestamp", timestamp);
        ret.put("signature", signature);
        return ret;
    }

    @RequestMapping(value = "index")
    public String index() {
        return "index";
    }
}